const db = wx.cloud.database()
const app = getApp();

Page({
  //页面的初始数据
  data: {
    showmine:false,//个人信息弹窗
    showModal: false, //管理弹出窗口
    userInfo: '',
    anonymous: "",
    login: "未知",
    isVIP: false,
    wenzhang: [],
    message: [],
    fenxiang: "false",
    stateId: "",
    bg_img: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/%E8%93%9D%E5%A4%A9%E7%A7%91%E6%99%AE3.0%E5%AD%98%E5%82%A8/personal/%E8%83%8C%E6%99%AF.png?sign=aa36c1b019755bd38ff0966520e08522&t=1645941910',
    //功能栏数据
    function: [{
        icon: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/mine/%E5%8A%9F%E8%83%BD/xiaoxi.png?sign=f0bf65b812bc8c244252c76284d17dd4&t=1635915631',
        title: '消息',
      },
      {
        icon: '/images/icon/icon1.png',
        title: '标题二',
      },
      {
        icon: '/images/icon/icon1.png',
        title: '标题三',
      },
      {
        icon: '/images/icon/icon1.png',
        title: '标题四',
      },
      {
        icon: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/admin/guanliyuan.png?sign=c5554adde457d942e94985ad26afc385&t=1641118991',
        title: '管 理',
      },
      {
        icon: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/%E8%93%9D%E5%A4%A9%E7%A7%91%E6%99%AE3.0%E5%AD%98%E5%82%A8/mine/%E6%B3%A8%E9%94%80.png?sign=581dc86a445d5ec570765032b698246e&t=1646228841',
        title: '注 销',
      }
    ]
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    let users = wx.getStorageSync('user')
    this.setData({
      userInfo: users
    })
  },
  //授权登录函数
  login() {
    let users = wx.getStorageSync('user')
    if (users == "") {
      var LoginState = true
      app.globalData.mealsList = LoginState; //全局变量输出
      wx.getUserProfile({
        desc: '是否同意授权登录？',
        success: res => {
          let users = res.userInfo
          wx.setStorageSync('user', users)
          //console.log(users)
          wx.showLoading({
            title: '登陆中',
            duration: 1500,
          })
          db.collection('users').add({
            data: {
              allow: true,
              ban: false,
              msgnb: [0, 0],
              online: true,
              wenzhang: [],
              message: [],
              pinglunguode: [],
              status: 1,
              weiguinb: 0,
              userinfo: {
                userphoto: users.avatarUrl,
                username: users.nickName,
                anonymous: "",
                isVIP: false,
                login: true,
              }
            }
          })
          wx.cloud.callFunction({
            name: 'login',
            data: {}
          }).then((res) => {
           // console.log("获取到openid:", res.result.openid);
            wx.setStorageSync('openid', res.result.openid)
          })
          //console.log(users)
          this.setData({
            userInfo: users
          })
          fail: res => {
            wx.showToast({
              title: '登录失败',
              duration: 2000,
            })
          }
        }
      })
    } else {
     this.setData({
       showmine:true
     })
    }
  },
  //登录退出函数
  loginout() {
    this.setData({
      userInfo: '',
    })
    wx.cloud.callFunction({
      name: 'login',
      data: {}
    }).then((res) => {
     // console.log("获取到openid:", res.result.openid);
      db.collection("users").where({
        _openid: res.result.openid
      }).get().then((res) => {
        this.id = res.data[res.data.length - 1]._id
        //console.log("首页登录取到的对应id的信息：", this.id);
        db.collection('users').doc(this.id).update({
          //该条记录为云数据库手动添加的记录
          data: {
            status: 0
          }
        }).then(res => {
          console.log(res)
        })
        //update()修改返回成功但无效，返回显示stats:{update: 0}
      })
    });
    wx.showLoading({
      title: '登陆中',
      duration: 1500,
    })
    wx.showToast({
      title: '退出成功',
      duration: 1500
    })
    wx.clearStorageSync() //清除缓存
  },
  //未登录说明
  loginError: function (e) {
    wx.showToast({
      title: '请先授权登录',
      icon: 'error',
      duration: 2000
    })
  },
  //消息查看功能函数
  message: function (e) {
    let that=this
    var id = e.currentTarget.dataset.id + 1
    //console.log('点击查看:', id)
    switch (id) {
      case 1:
        wx.navigateTo({
          url: '/forum/message/xiaoxi/xiaoxi'
        });
        break;
      case 2:
        wx.navigateTo({
          url: '/forum/message/shouquan/shouquan'
        });
        break;
      case 3:
        wx.navigateTo({
          url: '/forum/my/wd/wd'
        });
        break;
      case 4:
        wx.navigateTo({
          url: '/forum/my/wd/wd'
        });
        break;
      case 5:
        this.manger();
        break;
      case 6:
        wx.showModal({
          title: '提示',
          content: '确认要退出登录?',
          success (res) {
            if (res.confirm) {
              that.loginout();
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        break;
    }
  },
  //管理员模式
  manger: function () {
    this.submit()
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  outwindow: function () {
    this.setData({
      showModal: false,  
    })
  },
  account: function (e) {
    var number1 = e.detail.value
    this.data.number1 = number1
  },
  password: function (e) {
    var number2 = e.detail.value
    this.data.number2 = number2
  },
  //账户验证函数
  proving: function () {
    var account = this.data.number1
    var password = this.data.number2
    db.collection('Admin').where({
      type: 'admin'
    }).get({
      success: res => {
        //var name1=res.data[0].account
        var key1 = res.data[0].password
        var key2 = res.data[1].password
        if (account == 'admin') {
          if (password == key2 || password == key1) {
            wx.showLoading({
              title: '加载中',
              duration: 1500,
              mask: true
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '/admin/admin/admin',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: '密码错误',
              icon: 'error',
              duration: 2000
            })
          }
        } else {
          wx.showToast({
            title: '账号错误',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  //个人信息弹窗关闭
  getcloseM:function(){
  this.setData({
    showmine: false
  })
  },
  //生命周期函数--监听页面初次渲染完成
  onReady: function () {

  },

  //生命周期函数--监听页面显示
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //生命周期函数--监听页面卸载
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})